import req from '../../utils/request.js'
const app = getApp();
var appid = app.globalData.appid;
const uid = app.globalData.uid;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    actionSheetHidden: true,
    painting: {},
    shareImage: '',
    qrcode: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    let userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo)
    if (userInfo) {
      this.setData({
        userInfo: userInfo
      })
    }

    wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
      title: '加载中',
      icon: 'loading',
    });
    //获取祝福
    req.post('/api/bless/list', { 'code': uid, 'appid': appid }, false)
      .then(res => {
        wx.hideLoading();
        that.setData({
          // mainInfo: res.data.mainInfo,
          zanLog: res,
          zanNum: res.length,
          // slideList: res.data.slideList
        });
      })
  },
  openActionsheet: function() {
    var self = this;
    self.setData({
      actionSheetHidden: !self.data.actionSheetHidden
    });
  },
  listenerActionSheet: function() {
    var self = this;
    self.setData({
      actionSheetHidden: !self.data.actionSheetHidden
    })
  },
  createPoster: function() {
    wx.navigateTo({
      url: '/pages/poster/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    //获取祝福
    req.post('/sys/bless/list', { 'code': uid, 'appid': appid }, false)
      .then(res => {
        wx.hideLoading();
        that.setData({
          zanLog: res,
          zanNum: res.length,
        });
      })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this;
    return {
      title: "分享给好友吧",
      imageUrl: "",
      path: 'pages/index/index',
      success: function(res) {
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function(res) {
        // 转发失败
        wx.showToast({
          title: '分享取消',
        })
      }
    }
  },
  // 送上祝福
  bindgetuserinfo: function(e) {
    console.log(e.detail.userInfo)
    var that = this;
    if (e.detail.userInfo) {
      wx.setStorageSync('userInfo', e.detail.userInfo)
      that.setData({
        userInfo: e.detail.userInfo,
        authBtn: false
      })
      var userInfo = e.detail.userInfo;
      var name = userInfo.nickName;
      var face = userInfo.avatarUrl;
      var data =  {
        'uid': 1,
        'nickName': userInfo.nickName,
        'gender': userInfo.gender,
        'language': userInfo.language,
        'city': userInfo.city,
        'province': userInfo.province,
        'country': userInfo.country,
        'avatarUrl': userInfo.avatarUrl,
        'openid': app.globalData.userInfo.openid
      }
      req.post('/sys/bless/save', data, true)
        .then(res => {
          wx.hideLoading();
          that.setData({
            zanLog: res,
            zanNum: res.length,
          });
          wx.showModal({
            title: '提示',
            content: "感谢你的祝福",
            showCancel: false
          })
        })
    } else {
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 2000
      });
    }
  },
  // 送上祝福
  zan: function(event) {
    var that = this;
    var userInfo = that.data.userInfo;
    var data = {
      'uid': 1,
      'nickName': userInfo.nickName,
      'gender': userInfo.gender,
      'language': userInfo.language,
      'city': userInfo.city,
      'province': userInfo.province,
      'country': userInfo.country,
      'avatarUrl': userInfo.avatarUrl,
      'openid': app.globalData.userInfo.openid
    }
    req.post('/sys/bless/save', data, true)
      .then(res => {
        wx.hideLoading();
        that.setData({
          zanLog: res,
          zanNum: res.length,
        });
        wx.showModal({
          title: '提示',
          content: "感谢你的祝福",
          showCancel: false
        })
      })
  },
})